package com.bootdo.order.listener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.common.config.Constant;
import com.bootdo.common.configure.dao.DictDao;
import com.bootdo.common.configure.domain.DictDO;
import com.bootdo.common.redis.lock.CommonRedisLock;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.mq.RabbitUtil;
import com.bootdo.mq.SendSortUtil;
import com.bootdo.payee.dao.PayeeDao;
import com.bootdo.payee.dao.PaymentCodeDao;
import com.bootdo.payee.domain.PaymentCodeDO;

/**
 * @Desc 二维码监听
 * @date 2020/3/15
 *
 */
@Configuration
@EnableScheduling
public class PaymentCodeListener {

    private static final Logger logger = LoggerFactory.getLogger(PaymentCodeListener.class);

    @Scheduled(cron = "0 */1 * * * ?")
    private void paymentCodeTask(){
        logger.info("<========开始执行调度任务清除两单空单二维码=========>");
        PaymentCodeDao paymentCodeMapper = ApplicationContextRegister.getBean(PaymentCodeDao.class);
        PayeeDao payeeMapper = ApplicationContextRegister.getBean(PayeeDao.class);
        RabbitUtil rabbitUtil = ApplicationContextRegister.getBean(RabbitUtil.class);
        DictDao dictMapper = ApplicationContextRegister.getBean(DictDao.class);
        SendSortUtil sendSortUtil = ApplicationContextRegister.getBean(SendSortUtil.class);
        StringRedisTemplate stringRedisTemplate = ApplicationContextRegister.getBean(StringRedisTemplate.class);

        //两单空单下码--获取当前超过两单空单的二维码信息
        Set<Integer> set = new CopyOnWriteArraySet<>();
        List<PaymentCodeDO> payments = new ArrayList<>();
        DictDO dictDO = new DictDO();
        dictDO.setType("checkNum");
        dictDO.setIsShow(1);
        DictDO dict = dictMapper.getDictByType(dictDO);
        List<PaymentCodeDO> list = paymentCodeMapper.queryEmptyCode(Integer.parseInt(dict.getValue()));
        if (list.size() > 0){
            for (PaymentCodeDO paymentCode : list){
                set.add(paymentCode.getPassagewayId());
                //获取当前用户最新二维码
                if (1 == paymentCode.getReceivingAccount().intValue()){
                    payments.add(paymentCode);
                    CommonRedisLock commonRedisLock = new CommonRedisLock(stringRedisTemplate);
                    boolean f = commonRedisLock.searchListByValue(Constant.PASSAGEWAY + paymentCode.getPassagewayId(), String.valueOf(paymentCode.getUserIdCreate()));
                    if (f){
                        commonRedisLock.lRemove(Constant.PASSAGEWAY + paymentCode.getPassagewayId(), 0, String.valueOf(paymentCode.getUserIdCreate()));
                        JSONObject object = new JSONObject();
                        String queueName = Constant.QUEUE_START+paymentCode.getUserIdCreate();
                        object.put("code",3);
                        object.put("date", DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));
                        object.put("userId",paymentCode.getUserIdCreate());
                        object.put("msg","空单数量过多，请重新上传接单二维码");
                        object.put("payType", paymentCode.getPassagewayId());
                        rabbitUtil.sendToQueue(queueName,object.toJSONString());
                    }
                }
            }
            //修改用户排队状态
            if (payments.size() > 0){
                payeeMapper.exitLine(payments);
            }
            //删除二维码
            paymentCodeMapper.deleteCodeByObj(list);
            sendSortUtil.sendMsg(set);
        }
        logger.info("<========定时任务十分钟清除两单空单二维码=========>");
    }
}
